package myStudent;

import java.util.ArrayList;
import java.util.Scanner;
public class StudentManage {
    //主界面的编写
//思路：
//用输出语句完成主界面的编写
    public static void main(String[] args) {
        ArrayList<Student> array = new ArrayList<>();
        while (true) {
            System.out.println("        欢迎来到学生管理系统      ");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看所有学生");
            System.out.println("5 退出");
            System.out.println("请输入你的选择：");
            Scanner sc = new Scanner(System.in);
            String line = sc.nextLine();
            switch (line) {
                case "1":
                    System.out.println("添加学生：");
                    addStudent(array);
                    break;
                case "2":
                    System.out.println("删除学生：");
                    deleteStudent(array);
                    break;
                case "3":
                    System.out.println("修改学生：");
                    updateStudent(array);
                    break;
                case "4":
                    System.out.println("查看所有学生");
                    seeStudent(array);
                    break;
                case "5":
                    System.out.println("谢谢使用");
                    System.exit(0);//使虚拟机退出，循环不再执行；
            }
        }
    }
    //添加学生编写：
    public static void addStudent(ArrayList<Student> arry){
        Scanner sc= new Scanner(System.in);
        String sid;
        while (true) {
            System.out.println("请输入学生学号：");
             sid = sc.nextLine();
            boolean flag = ifuse(arry, sid);
            if (flag) {
                System.out.println("你输入的学号已被占用，请重新输入");
            }else {
                break;
            }
        }
        System.out.println("请输入学生姓名：");
        String name = sc.nextLine();
        System.out.println("请输入学生年龄：");
        String age = sc.nextLine();
        System.out.println("请输入学生居住地：");
        String address =sc.nextLine();
        Student s = new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);
        arry.add(s);
        System.out.println("添加成功");
    }
    //判断学号是否重复
    public static boolean ifuse(ArrayList<Student> arry,String sid){
        boolean flag = false;
        for(int i = 0;i<arry.size();i++){
            Student s = arry.get(i);
            if(s.getSid().equals(sid)){
                flag = true;
                break;
            }
        }
        return  flag;
    }
    //查看学生编写：
    public static void seeStudent(ArrayList<Student> arry){
        if (arry.size()==0){
            System.out.println("无信息，请先添加信息后再查询");
            return;//为了程序不再往下执行；
        }
        System.out.println("学号\t姓名\t年龄\t居住地");
        for(int i = 0;i<arry.size();i++){
            Student s = arry.get(i);
            System.out.println(s.getSid()+"\t\t"+s.getName()+" \t"+s.getAge()+"岁"+" \t\t"+s.getAddress());
        }
    }
    //删除学生编写
    public static void deleteStudent(ArrayList<Student> arry){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除学生的学号：");
        String sid = sc.nextLine();
       /* for(int i = 0;i<arry.size();i++){
            Student s = arry.get(i);
            if(s.getSid().equals(sid)){
                arry.remove(i);
                break;
            }
        }
        System.out.println("删除学生成功");
        */
       //学号不存在问题的解决：
        int index = -1;
        for (int i = 0;i<arry.size();i++) {
            Student s = arry.get(i);
            if (s.getSid().equals(sid)) {
                index = i;
                break;
            }
        }
            if(index == -1){
                System.out.println("你输入的学号有误，请重新输入");
            } else {
                arry.remove(index);
                System.out.println("删除成功");
            }
    }
    //修改学生编写
    public static void updateStudent(ArrayList<Student> arry){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的学生的学号：");
        String sid = sc.nextLine();
        System.out.println("请输入新姓名：");
        String name = sc.nextLine();
        System.out.println("请输入新年龄：");
        String age = sc.nextLine();
        System.out.println("请输入新地址：");
        String address = sc.nextLine();
        Student s = new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);
        for (int i = 0;i<arry.size();i++){
            Student student = arry.get(i);
            if(student.getSid().equals(sid)){
                arry.set(i,s);
                break;
            }
        }
        System.out.println("修改学生成功");
    }
}


